/***********************************************************************//**
 *   \file FilterEqualizer.cpp		\author belot nicolas (karrakis)           
 *   \brief define an Equalizer
     Apply an Equalizer via a DFT		                                   
 ************************************************************************//*
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "FilterEqualizer.h"
#include <iostream>
namespace libtrckr {
string FilterEqualizer::HowToTune(){
	return "Multiband X -10 +10";
}

FilterEqualizer::FilterEqualizer():FilterDFT(){
	size=0;
	eqy=0;
	eqx=0;
	bitrate=0;
	csize=0;
}


FilterEqualizer::FilterEqualizer(const FilterEqualizer& f):FilterDFT(f),size(f.size),bitrate(f.bitrate){
	eqy = (double*)malloc(sizeof(double)*size);
	eqx = (double*)malloc(sizeof(double)*size);
	for(unsigned int i=0;i<size;i++){
		eqy[i]=f.eqy[i];
		eqx[i]=f.eqx[i];
	}
	csize=0;
}

FilterEqualizer::~FilterEqualizer(){
	free(eqx);
	free(eqy);
}

void FilterEqualizer::Init(){}

void FilterEqualizer::Tune(DynArray<int> conf){
	if(eqy!=0) free(eqy);
	if(eqx!=0) free(eqy);
	eqx=(double*)malloc(sizeof(double)*(conf[0]/2) );
	eqy=(double*)malloc(sizeof(double)*(conf[0]/2) );
	unsigned int j=0;
	for(unsigned int i=0;i<conf[0];i+=2){
		eqy[j]=conf[i+1]/100.0;
		eqx[j]=conf[i+2];
		j++;
	}
	bitrate=conf[1+conf[0]];
	size=j;
}

Buffer &FilterEqualizer::Preview(Buffer& b){
	/* Interpolate an Equalizer to the size of the buffer
	if needed*/
	if( csize!= b.size()){
		double *tmpc = (double*)malloc(sizeof(double)*b.size());
		csize=b.size();
		Utility::Interpolate(Utility::MONOTONE_CUBIC, eqx, eqy, size, tmpc,csize, bitrate/csize);
		for(uint i=0;i<csize;i++){
			tmpc[i]=pow(10,tmpc[i]/20.0); //transform coef from dB to Amplitude
		}
		DFT::setData(tmpc,tmpc,csize);	
		free(tmpc);
	}
	FilterDFT::Preview(b);
}

double *FilterEqualizer::Preview(double* data, int size){
	if( csize!= size){
		double *tmpc = (double*)malloc(sizeof(double)*size);
		csize=size;
		Utility::Interpolate(Utility::MONOTONE_CUBIC, eqx, eqy, size, tmpc,csize, bitrate/csize);
		DFT(tmpc,tmpc,csize);	
		free(tmpc);
	}
	FilterDFT::Preview(data,size);
}

}

